package com.example.administrator.yuejian_15.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.yuejian_15.R;
import com.example.administrator.yuejian_15.bean.RegistBean;
import com.example.administrator.yuejian_15.listener.TListener;
import com.example.administrator.yuejian_15.model.RegistModel;


public class RegistActivity extends BaseActivity implements View.OnClickListener {
    EditText re_phone,re_username,re_password,re_password2,re_email;
    CheckBox re_tyyjxy;
    Button re_regist,re_fh;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_regist);

        re_phone=(EditText)findViewById(R.id.re_phone);
        re_password=(EditText)findViewById(R.id.re_password);
        re_password2=(EditText)findViewById(R.id.re_password2);
        re_username=(EditText)findViewById(R.id.re_username);
        re_email=(EditText)findViewById(R.id.re_email);

        re_tyyjxy=(CheckBox)findViewById(R.id.re_tyyjxy);

        re_regist=(Button)findViewById(R.id.re_regist);
        re_fh = (Button)findViewById(R.id.re_fh);


    }

    @Override
    void initEvent() {
        re_fh.setOnClickListener(this);//返回监听
        re_regist.setOnClickListener(this);

    }

    @Override
    void initDate() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.re_fh://返回监听实现
                finish();
                break;
            case R.id.re_regist:
                String phone = re_phone.getText().toString().trim();
                String password = re_password.getText().toString().trim();
                String password2 = re_password2.getText().toString().trim();
                String username = re_username.getText().toString().trim();
                String email = re_email.getText().toString().trim();
                if (phone.equals("")||password.equals("")||password2.equals("")||email.equals("")||username.equals("")){
                    Toast.makeText(RegistActivity.this,"填写信息不能为空",Toast.LENGTH_SHORT).show();
                }else if (!password.equals(password2)){
                    Toast.makeText(RegistActivity.this,"两次密码输入不一致",Toast.LENGTH_SHORT).show();
                }else if(!re_tyyjxy.isChecked()){
                    Toast.makeText(RegistActivity.this,"同意协议方可注册",Toast.LENGTH_SHORT).show();
                }
                else{
                    RegistModel registModel=new RegistModel();
                    registModel.regist(username,password,phone,email,registBeanTListener);

                }
        }
    }

    TListener<RegistBean> registBeanTListener = new TListener<RegistBean>() {
        @Override
        public void onResponse(RegistBean registBean) {
            String success = registBean.getSuccess();
            if (success.equals("0")){
                Toast.makeText(RegistActivity.this,"用户已存在",Toast.LENGTH_SHORT).show();
            }
            else if (success.equals("1")){
                Toast.makeText(RegistActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                String username = re_username.getText().toString().trim();

                Intent intent = new Intent(RegistActivity.this,LoginActivity.class);
                intent.putExtra("username",username);
                setResult(RESULT_OK,intent);
                finish();
            }
            else if (success.equals("3")){
                Toast.makeText(RegistActivity.this,"该电话号码已被注册",Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(RegistActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(RegistActivity.this,"注册失败，或网络错误",Toast.LENGTH_SHORT).show();

        }
    };
}
